Monitor and Report the Disk Space through SCRIPT using PowerShell or VB
Dear All,
We are having environment of more then 200 Production Servers which are Critical hence we need to Monitor and Report the Disk Space through SCRIPT using PowerShell or VB. If any body has already been done , Kindly share it.
Thanks & Regards,
Amit Satam
March 16th, 2012 3:08pm
from http://www.youdidwhatwithtsql.com/check-disk-space-with-powershell-2/195
# Issue warning if % free disk space is less
$percentWarning = 15;
# Get server list
$servers = Get-Content "$Env:USERPROFILE\serverlist.txt";
$datetime = Get-Date -Format "yyyyMMddHHmmss";
# Add headers to log file
Add-Content "$Env:USERPROFILE\server disks $datetime.txt" "server,deviceID,size,freespace,percentFree";
foreach($server in $servers)
{
# Get fixed drive info
$disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3";
foreach($disk in $disks)
{
$deviceID = $disk.DeviceID;
[float]$size = $disk.Size;
[float]$freespace = $disk.FreeSpace;
$percentFree = [Math]::Round(($freespace / $size) * 100, 2);
$sizeGB = [Math]::Round($size / 1073741824, 2);
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
$colour = "Green";
if($percentFree -lt $percentWarning)
{
$colour = "Red";
}
Write-Host -ForegroundColor $colour "$server $deviceID percentage free space = $percentFree";
Add-Content "$Env:USERPROFILE\server disks $datetime.txt" "$server,$deviceID,$sizeGB,$freeSpaceGB,$percentFree";
}
}
Some basic info is there http://www.computerperformance.co.uk/powershell/powershell_wmi_disk.htm
Free Windows Admin Tool Kit Click here and download it now
March 16th, 2012 3:13pm
Have a look at this also. It gives a pretty graphical display.
#requires -version 2.0
#use parameter drive report to html.ps1 computer1,computer2 or a computer list file
#change file path and name on line 7 below to reflect name and path of computer list file using.
#script will open web browser with current report when completed.
Param (
$computers = (Get-Content "C:\Scripts\Computers.txt")
)
$Title="Hard Drive Report to HTML"
#embed a stylesheet in the html header
$head = @"
<mce:style><!--
mce:0
--></mce:style><style _mce_bogus="1"><!--
mce:0
--></style>
<Title>$Title</Title>
<br>
"@
#define an array for html fragments
$fragments=@()
#get the drive data
$data=Get-WmiObject -Class Win32_logicaldisk -filter "drivetype=3" -computer $computers
#group data by computername
$groups=$Data | Group-Object -Property SystemName
#this is the graph character
[string]$g=[char]9608
#create html fragments for each computer
#iterate through each group object
ForEach ($computer in $groups) {
$fragments+="<H2>$($computer.Name)</H2>"
#define a collection of drives from the group object
$Drives=$computer.group
#create an html fragment
$html=$drives | Select @{Name="Drive";Expression={$_.DeviceID}},
@{Name="SizeGB";Expression={$_.Size/1GB -as [int]}},
@{Name="UsedGB";Expression={"{0:N2}" -f (($_.Size - $_.Freespace)/1GB) }},
@{Name="FreeGB";Expression={"{0:N2}" -f ($_.FreeSpace/1GB) }},
@{Name="Usage";Expression={
$UsedPer= (($_.Size - $_.Freespace)/$_.Size)*100
$UsedGraph=$g * ($UsedPer/2)
$FreeGraph=$g* ((100-$UsedPer)/2)
#I'm using place holders for the < and > characters
"xopenFont color=Redxclose{0}xopen/FontxclosexopenFont Color=Greenxclose{1}xopen/fontxclose" -f $usedGraph,$FreeGraph
}} | ConvertTo-Html -Fragment
#replace the tag place holders. It is a hack but it works.
$html=$html -replace "xopen","<"
$html=$html -replace "xclose",">"
#add to fragments
$Fragments+=$html
#insert a return between each computer
$fragments+="<br>"
} #foreach computer
#add a footer
$footer=("<br><I>Report run {0} by {1}\{2}<I>" -f (Get-Date -displayhint date),$env:userdomain,$env:username)
$fragments+=$footer
#write the result to a file
ConvertTo-Html -head $head -body $fragments | Out-File $Path
.\drivereport.htm
Regards, ~P MCSE, MCITP, MCTS, MCP, CCNA
March 16th, 2012 3:44pm
Dear Gandalf,
If I want to Test the above Script in one of the Machine , Do I need to simply execute the the PS Script or any alteration are required in it.
Thanks & Regards,
Amit Satam
Free Windows Admin Tool Kit Click here and download it now
March 16th, 2012 8:31pm
to run it against one machine type c:\script_path\'hard drive report to html.ps1' computer1 or use a list of computers in a text file with each computer name on a separate line. use
change line 7 $computers
=
(Get-Content
"C:\Scripts\Computers.txt")
to
point to your computer list name and path
the results will open in your default browser
Regards, ~P MCSE, MCITP, MCTS, MCP, CCNA
March 16th, 2012 10:16pm
Hi,
Please also refer to scripting center for the similar scripts:
Disk Space Monitoring - HTML EMAIL Report
http://gallery.technet.microsoft.com/scriptcenter/6e935887-6b30-4654-b977-6f5d289f3a63
Monitor Free Disk Space Information on a Computer
http://gallery.technet.microsoft.com/scriptcenter/04c29c84-5ecc-4bf6-8dd4-2940db63d9f3
List Available Disk Space
http://gallery.technet.microsoft.com/scriptcenter/7fa38863-ad6f-4f46-ac91-9b7d4a30f52b
Disk Space monitoring
http://gallery.technet.microsoft.com/scriptcenter/fd4f5235-1a80-41ed-87e2-189278fd376c
If you encounter any difficulties when customizing the scripts, you may submit a new question in The Official Scripting Guys Forum! which is a best resource for scripting related issues.
The Official Scripting Guys Forum!
http://social.technet.microsoft.com/Forums/en/ITCG/threads
Regards,
Arthur Li
TechNet Community Support
Free Windows Admin Tool Kit Click here and download it now
March 20th, 2012 8:01am
I'm getting these errors when running this .ps1 file
Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument
that is not null or empty and then try the command again.
At C:\test.ps1:27 char:77
+ $data=Get-WmiObject -Class Win32_logicaldisk -filter "drivetype=3" -computer <<<< $computers
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At C:\test.ps1:75 char:56
+ ConvertTo-Html -head $head -body $fragments | Out-File <<<< $Path
+ CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileComm
and
The term '.\drivereport.htm' is not recognized as the name of a cmdlet, function, script file, or operable program. Che
ck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\test.ps1:77 char:18
+ .\drivereport.htm <<<<
+ CategoryInfo : ObjectNotFound: (.\drivereport.htm:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
June 27th, 2012 9:33am